﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class PropertyValueDal
    {
        #region PropertyValueDal private
        private static void SetInfo(IDataRecord reader, ref PropertyValueEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<PropertyValueEntity> categoryList)
        {
            while (reader.Read())
            {
                var o = new PropertyValueEntity();
                SetInfo(reader, ref o);
                categoryList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<PropertyValueEntity> GetListByPropertyId(int propertyId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstPropertyValue = new List<PropertyValueEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "PropertyValue_GetListByPropertyId", propertyId);
                SetInfoList(ref reader, ref lstPropertyValue);
                return lstPropertyValue;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "PropertyValue_GetListByPropertyId: {0}", ex.Message));
            }
        }

        public static List<PropertyValueEntity> GetAllListByPropertyId(int propertyId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstPropertyValue = new List<PropertyValueEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "PropertyValue_GetAllListByPropertyId", propertyId);
                SetInfoList(ref reader, ref lstPropertyValue);
                return lstPropertyValue;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "PropertyValue_GetAllListByPropertyId: {0}", ex.Message));
            } 
        }

        public static void Create(PropertyValueEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                    SqlHelper.ExecuteNonQuery(
                    DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "PropertyValue_Create", o.PropertyId, o.Name, o.Priority);            
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "PropertyValue_Create: {0}", ex.Message));
            }
        }

        public static void Delete(int propertyId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(
                DbCommon.ConnectionString,
                DbCommon.DatabaseSchema + "PropertyValue_Delete_ByPropertyId", propertyId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "PropertyValue_Delete_ByPropertyId: {0}", ex.Message));
            }
        }

    }
}
